﻿using StartEAS_API.Utils;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.DAL.WorkerD
{
    public class WorkerDAL
    {
        private readonly StarsEASContext _context;
        public WorkerDAL(StarsEASContext context) { 
            _context = context;
        }
        public ResultUtils GetAllWorker() {
            StarsEASContext _context = new StarsEASContext();
            List<Worker> workerList = _context.Workers.ToList();
            if (workerList == null)
            {
                AppLog.WriteError($"查询老师信息---未查询到老师信息", true);
                return new ResultUtils() { code = 500, message = "未查询到老师信息" };
            }
            AppLog.WriteInfo($"查询老师信息---共查询到{workerList.Count()}条老师信息", true);
            return new ResultUtils() { code = 200, message = workerList };
        }

        public ResultUtils InsertWorker(Worker data) {
            StarsEASContext _context = new StarsEASContext();
            var wor = _context.Workers.OrderByDescending(i => i.WorkerId).FirstOrDefault();
            int workerId = wor == null ? 9001 : int.Parse(wor.WorkerId) + 1;
            data.WorkerId = workerId.ToString();
            User user = new User()
            {
                Username = data.WorkerId.ToString(),
                Password = Utils.GenerateMD5(data.WorkerCarid.Substring(data.WorkerCarid.Length-6, 6)),
                PositionId = data.PositionId,
            };
            _context.Users.Add(user);
            _context.Workers.Add(data);
            int result = _context.SaveChanges();
            if (result != 2)
            {
                AppLog.WriteError($"新增教职工信息---新增{workerId}-{data.WorkerName}-{user.Password}出现异常", true);
                return new ResultUtils() { code = 500, message = "教职工信息或用户信息出现异常" };
            }
            AppLog.WriteInfo($"新增教职工信息---新增{workerId}-{data.WorkerName}-{user.Password}成功", true);
            return new ResultUtils() { code = 200, message = data };
        }

        public ResultUtils UpdateWorker(Worker data) {
            StarsEASContext _context = new StarsEASContext();
            if (data == null) {
                AppLog.WriteError($"修改教职工信息---未提交教职工信息", true);
                return new ResultUtils() { code = 500, message = "请提交教职工信息" };
            }
            var worker = _context.Workers.Where<Worker>(w=>w.WorkerId==data.WorkerId).FirstOrDefault();
            if (worker == null) {
                AppLog.WriteError($"修改教职工信息---未查询到{data.WorkerId}的教职工信息", true);
                return new ResultUtils() { code = 500, message = "教职工编号错误" };
            }
            worker.WorkerPhone = data.WorkerPhone;
            worker.WorkerCarid = data.WorkerCarid;
            worker.WorkerEmail = data.WorkerEmail;
            worker.WorkerLocal = data.WorkerLocal;
            worker.PositionId = data.PositionId;
            worker.WorkerName = data.WorkerName;
            worker.WorkerAge = data.WorkerAge;
            worker.WorkerSex = data.WorkerSex;
            worker.WorkerbackTime = data.WorkerbackTime;
            int result = _context.SaveChanges();
            if (result == 0) {
                AppLog.WriteError($"修改教职工信息---修改失败或与原数据相同", true);
                return new ResultUtils() { code = 500, message = "修改失败或与原数据相同" };
            }
            AppLog.WriteInfo($"修改教职工信息---修改{worker.WorkerId}信息成功",true);
            return new ResultUtils() { code = 200, message = result };
        }

        public Worker GetWorker(string workerId) {
            StarsEASContext _context = new StarsEASContext();
            Worker worker = _context.Workers.Where(w => w.WorkerId == workerId).FirstOrDefault();
            if (worker == null) {
                AppLog.WriteError($"查询教职工信息---根据{workerId}未查询到教职工信息", true);
                return null;
            }
            AppLog.WriteInfo($"查询教职工信息---查询完成", true);
            return worker;
        }
    }
}
